var xHRObject = false;
if (window.ActiveXObject) {
	xHRObject = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
	xHRObject = new XMLHttpRequest();
}


function getData() {
	if (xHRObject.readyState == 4 && xHRObject.status == 200) {
        var serverResponse = xHRObject.responseXML;
		if (serverResponse != null) {
			var itemsNode = serverResponse.getElementsByTagName('items');
			if (itemsNode.length > 0) {
				var attribute = itemsNode[0].attributes[0].value;
				if (attribute == 'loadCategories') {
					var categoryIDNodes = serverResponse.getElementsByTagName('categories_id');
					var categoryNameNodes = serverResponse.getElementsByTagName('categories_name');
					var selectElement = document.getElementById('categoryID');
					
					if (window.ActiveXObject) {
						selectElement.innerHTML = '';
						var option = document.createElement('<option value="">');
						var text = document.createTextNode('--请选择分类--');
						option.appendChild(text);
						selectElement.appendChild(option);
					} 
					else if (window.XMLHttpRequest){
						selectElement.innerHTML = '<option value="">--请选择分类--</option>';
					}
					
					//emptying imageList
					var imageListElement = document.getElementById('imageList');
					imageListElement.innerHTML = '';

					//removing the controls iframe
					var controlElement = document.getElementById('controls');
					controlElement.innerHTML = '';

					//removing the displayed image
					var imageDisplayElement = document.getElementById('imageDisplay');
					imageDisplayElement.innerHTML = '';

					//removing orders and newTitle
					document.getElementById('order').value="";
					document.getElementById('newTitle').value="";
					
					for (i=0; i<categoryIDNodes.length; i++) {
				    if (window.ActiveXObject) {		
							var option = document.createElement('<option value="'+categoryIDNodes[i].text+'">');
							var text = document.createTextNode(categoryNameNodes[i].text);
							option.appendChild(text);
							selectElement.appendChild(option);
						}
						else {
							selectElement.innerHTML += '<option value="' + categoryIDNodes[i].textContent + '">' + categoryNameNodes[i].textContent + '</option>';	
						}
			    }
				}
				else if (attribute == 'loadProducts') {
					var productIDNodes = serverResponse.getElementsByTagName('products_id');
					var productNameNodes = serverResponse.getElementsByTagName('products_name');
					var selectElement = document.getElementById('productID');
					
					if (window.ActiveXObject) {
						selectElement.innerHTML = '';
						var option = document.createElement('<option value="">');
						var text = document.createTextNode('--请选择商品--');
						option.appendChild(text);
						selectElement.appendChild(option);
					} 
					else if (window.XMLHttpRequest){
						selectElement.innerHTML = '<option value="">--请选择商品--</option>';
					}
					
					//emptying imageList
					var imageListElement = document.getElementById('imageList');
					imageListElement.innerHTML = '';

					//removing orders and newTitle
					document.getElementById('order').value="";
					document.getElementById('newTitle').value="";
					
					//removing the controls iframe
					var controlElement = document.getElementById('controls');
					controlElement.innerHTML = '';

					//removing the displayed image
					var imageDisplayElement = document.getElementById('imageDisplay');
					imageDisplayElement.innerHTML = '';
					
					for (i=0; i<productIDNodes.length; i++) {
				    if (window.ActiveXObject) {		
							var option = document.createElement('<option value="'+productIDNodes[i].text+'">');
							var text = document.createTextNode(productNameNodes[i].text);
							option.appendChild(text);
							selectElement.appendChild(option);
						}
						else {
							selectElement.innerHTML += '<option value="' + productIDNodes[i].textContent + '">' + productNameNodes[i].textContent + '</option>';	
						}
			    }
				}
				else if (attribute == 'loadAttributes') {
					var optionIDNodes = serverResponse.getElementsByTagName('products_options_id');
					var optionNameNodes = serverResponse.getElementsByTagName('products_options_name');
					var selectElement = document.getElementById('attributeID');
					
					if (window.ActiveXObject) {	
						selectElement.innerHTML = '';
						var option = document.createElement('<option value="">');
						var text = document.createTextNode('--请选择选项名--');
						option.appendChild(text);
						selectElement.appendChild(option);
					} 
					else if (window.XMLHttpRequest){
						selectElement.innerHTML = '<option value="">--请选择选项名--</option>';
					}
					
					var selectElement2 = document.getElementById('optionID');
					if (window.ActiveXObject) {	
						selectElement2.innerHTML = '';
						var option = document.createElement('<option value="">');
						var text = document.createTextNode('--请选择选项值--');
						option.appendChild(text);
						selectElement2.appendChild(option);
					}
					else {
						selectElement2.innerHTML = '<option value="">--请选择选项值--</option>';
					}

					//emptying imageList
					var imageListElement = document.getElementById('imageList');
					imageListElement.innerHTML = '';

					//removing the controls iframe
					var controlElement = document.getElementById('controls');
					controlElement.innerHTML = '';

					//removing the displayed image
					var imageDisplayElement = document.getElementById('imageDisplay');
					imageDisplayElement.innerHTML = '';

					//removing orders and newTitle
					document.getElementById('order').value="";
					document.getElementById('newTitle').value="";
					
					for (i=0; i<optionIDNodes.length; i++) {
				    if (window.ActiveXObject) {		
							var option = document.createElement('<option value="'+optionIDNodes[i].text+'">');
							var text = document.createTextNode(optionNameNodes[i].text);
							option.appendChild(text);
							selectElement.appendChild(option);
						}
						else {
							selectElement.innerHTML += '<option value="' + optionIDNodes[i].textContent + '">' + optionNameNodes[i].textContent + '</option>';	
						}
			    }				
				}
				else if (attribute == 'loadOptions') {
					var optionValueIDNodes = serverResponse.getElementsByTagName('products_attributes_id');
					var optionValueNameNodes = serverResponse.getElementsByTagName('products_options_values_name');
					var selectElement = document.getElementById('optionID');
					
					if (window.ActiveXObject) {	
						selectElement.innerHTML = '';
						var option = document.createElement('<option value="">');
						var text = document.createTextNode('--请选择选项值--');
						option.appendChild(text);
						selectElement.appendChild(option);
					} 
					else if (window.XMLHttpRequest){
						selectElement.innerHTML = '<option value="">--请选择选项值--</option>';
					}
					
					//emptying imageList
					var imageListElement = document.getElementById('imageList');
					imageListElement.innerHTML = '';

					//removing the controls iframe
					var controlElement = document.getElementById('controls');
					controlElement.innerHTML = '';

					//removing the displayed image
					var imageDisplayElement = document.getElementById('imageDisplay');
					imageDisplayElement.innerHTML = '';

					//removing orders and newTitle
					document.getElementById('order').value="";
					document.getElementById('newTitle').value="";
					
					for (i=0; i<optionValueIDNodes.length; i++) {
						if (window.ActiveXObject) {		
							var option = document.createElement('<option value="'+optionValueIDNodes[i].text+'">');
							var text = document.createTextNode(optionValueNameNodes[i].text);
							option.appendChild(text);
							selectElement.appendChild(option);
						}
						else {
							selectElement.innerHTML += '<option value="' + optionValueIDNodes[i].textContent + '">' + optionValueNameNodes[i].textContent + '</option>';	
						}
					}				
				}
				else if (attribute == 'loadImages' || attribute == 'delImage') {
					//removing the displayed image
					var imageDisplayElement = document.getElementById('imageDisplay');
					imageDisplayElement.innerHTML = '';
					
					//removing orders and newTitle
					document.getElementById('order').value="";
					document.getElementById('newTitle').value="";
					
					//displaying the controls iframe
					var controlElement = document.getElementById('controls');
					controlElement.innerHTML = '<iframe frameborder="0" scrolling="no" src ="image_uploader.php" width="100%" height="300px"></iframe>';
					
					//get returned XML
					var imageIDNodes = serverResponse.getElementsByTagName('image_id');
					var imagePathNodes = serverResponse.getElementsByTagName('image_path');
					var imageSortOrderNodes = serverResponse.getElementsByTagName('image_sort_order');
					var imageTitleNodes = serverResponse.getElementsByTagName('image_title');
					
					//display the returned XML to imageList
					var selectElement = document.getElementById('imageList');
					selectElement.innerHTML = '';
					if (imageIDNodes.length == 0) {
						if (window.ActiveXObject) {		
							var option = document.createElement('<option value="">');
							var text = document.createTextNode('--该属性没有图片--');
							option.appendChild(text);
							selectElement.appendChild(option);
						}
						else {
							selectElement.innerHTML = '<option value="">--该属性没有图片--</option>';
						}
					}
					for (i=0; i<imageIDNodes.length; i++) {
						if (window.ActiveXObject) {		
							var option = document.createElement('<option title="' + imagePathNodes[i].text + '" value="' + imageIDNodes[i].text + '">');
							var text = document.createTextNode(imageTitleNodes[i].text);
							option.appendChild(text);
							selectElement.appendChild(option);
						}
						else {
							selectElement.innerHTML += '<option title="' + imagePathNodes[i].textContent + '" value="' + imageIDNodes[i].textContent + '">' + imageTitleNodes[i].textContent + '</option>';
						}
			        }
				}
				else if (attribute == 'selectImage') {
					var imagePathNodes = serverResponse.getElementsByTagName('image_path');
					var imageTitleNodes = serverResponse.getElementsByTagName('image_title');
					var imageSortNodes = serverResponse.getElementsByTagName('image_sort_order');
					
					var selectElement = document.getElementById('imageDisplay');
					var orderElement = document.getElementById('order');
					var titleElement = document.getElementById('newTitle');
					
					selectElement.innerHTML = '';
					
					if (imagePathNodes.length > 0) {
						if (window.ActiveXObject) {
							var image = document.createElement('<img src="../' + imagePathNodes[0].text + '"/>');
							selectElement.appendChild(image);
							orderElement.value = imageSortNodes[0].text;
							titleElement.value = imageTitleNodes[0].text;
						}
						else {
							selectElement.innerHTML += '<img src="../' + imagePathNodes[0].textContent + '"/>';	
							orderElement.value = imageSortNodes[0].textContent;
							titleElement.value = imageTitleNodes[0].textContent;
						}
					}
				}
				else if (attribute =='update') {
					//get returned XML
					var imageIDNodes = serverResponse.getElementsByTagName('image_id');
					var imagePathNodes = serverResponse.getElementsByTagName('image_path');
					var imageTitleNodes = serverResponse.getElementsByTagName('image_title');
					var imageSortOrderNodes = serverResponse.getElementsByTagName('image_sort_order');
					
					//display the returned XML to imageList
					var selectElement = document.getElementById('imageList');
					selectElement.innerHTML = '';
					if (imageIDNodes.length == 0) {
						if (window.ActiveXObject) {
							var option = document.createElement('<option value="">');
							var text = document.createTextNode('--该属性没有图片--');
							option.appendChild(text);
							selectElement.appendChild(option);
						}
						else {
							selectElement.innerHTML = '<option value="">--该属性没有图片--</option>';
						}
					}
					for (i=0; i<imageIDNodes.length; i++) {
						if (window.ActiveXObject) {		
							var option = document.createElement('<option value="' + imageIDNodes[i].text + '" title="' + imagePathNodes[i].text + '">');
							var text = document.createTextNode(imageTitleNodes[i].text);
							option.appendChild(text);
							selectElement.appendChild(option);
						}
						else {
							selectElement.innerHTML += '<option value="' + imageIDNodes[i].textContent + '">' + imageTitleNodes[i].textContent + '</option>';
						}
					}
				}
				else if (attribute == 'loadImagesFromIframe') {
					//removing the displayed image
					var imageDisplayElement = window.parent.document.getElementById('imageDisplay');
					imageDisplayElement.innerHTML = '';
					
					//get returned XML
					var imageIDNodes = serverResponse.getElementsByTagName('image_id');
					var imagePathNodes = serverResponse.getElementsByTagName('image_path');
					var imageTitleNodes = serverResponse.getElementsByTagName('image_title');
					var imageSortOrderNodes = serverResponse.getElementsByTagName('image_sort_order');
					
					//display the returned XML to imageList
					var selectElement = window.parent.document.getElementById('imageList');
					selectElement.innerHTML = '';
					if (imageIDNodes.length == 0) {
						selectElement.innerHTML = '<option value="">--该属性没有图片--</option>';
					}
					for (i=0; i<imageIDNodes.length; i++) {
						if (window.ActiveXObject) {		
							var option = document.createElement('<option value="' + imageIDNodes[i].text + '" title="' + imagePathNodes[i].text + '">');
							var text = document.createTextNode(imageTitleNodes[i].text);
							option.appendChild(text);
							selectElement.appendChild(option);
						}
						else {
							selectElement.innerHTML += '<option title="' + imagePathNodes[i].textContent + '" value="' + imageIDNodes[i].textContent + '">' + imageTitleNodes[i].textContent + '</option>';
						}
					}
					//displaying the controls iframe
					var controlElement = window.parent.document.getElementById('controls');
					controlElement.innerHTML = '<iframe frameborder="0" scrolling="no" src ="image_uploader.php" width="100%" height="300px"></iframe>';
				}
			}
		}
    }
}

function loadCategories() {
/*
	if (window.ActiveXObject) {
		xHRObject.open("GET","AJAX_servers/AJAX_image_swapper_server.php?action=loadCategories&id=" + Number(new Date), true);	
		xHRObject.onreadystatechange = getData;
	}
	else if (window.XMLHttpRequest) {
		xHRObject.onreadystatechange = getData;
		xHRObject.open("GET","AJAX_servers/AJAX_image_swapper_server.php?action=loadCategories&id=" + Number(new Date), true);	
	}
	xHRObject.send(null);   
*/
}

function selectCategoryID() {
	var categoryID = document.getElementById('categoryID').value;
	if (window.ActiveXObject) {
		xHRObject.open("GET","AJAX_servers/AJAX_image_swapper_server.php?action=loadProducts&category_id=" + categoryID + "&id=" + Number(new Date), true);	
		xHRObject.onreadystatechange = getData;
	}
	else if (window.XMLHttpRequest) {
		xHRObject.onreadystatechange = getData;
		xHRObject.open("GET","AJAX_servers/AJAX_image_swapper_server.php?action=loadProducts&category_id=" + categoryID + "&id=" + Number(new Date), true);	
	}
	xHRObject.send(null);   
}

function selectProductID() {
	var productID = document.getElementById('productID').value;
	if (window.ActiveXObject) {
		xHRObject.open("GET","AJAX_servers/AJAX_image_swapper_server.php?action=loadAttributes&product_id=" + productID + "&id=" + Number(new Date), true);	
		xHRObject.onreadystatechange = getData;
	}
	else if (window.XMLHttpRequest) {
		xHRObject.onreadystatechange = getData;
		xHRObject.open("GET","AJAX_servers/AJAX_image_swapper_server.php?action=loadAttributes&product_id=" + productID + "&id=" + Number(new Date), true);	
	}
	xHRObject.send(null);   
}

function selectAttributeID() {
	var productID = document.getElementById('productID').value;
	var attributeID = document.getElementById('attributeID').value;
	if (window.ActiveXObject) {
		xHRObject.open("GET","AJAX_servers/AJAX_image_swapper_server.php?action=loadOptions&product_id=" + productID + "&attribute_id=" + attributeID + "&id=" + Number(new Date), true);	
		xHRObject.onreadystatechange = getData;
	}
	else if (window.XMLHttpRequest) {
		xHRObject.onreadystatechange = getData;
		xHRObject.open("GET","AJAX_servers/AJAX_image_swapper_server.php?action=loadOptions&product_id=" + productID + "&attribute_id=" + attributeID + "&id=" + Number(new Date), true);	
	}
	xHRObject.send(null);   
}

function selectOptionID() {
	var productID = document.getElementById('productID').value;
	var attributeID = document.getElementById('attributeID').value;
	var optionID = document.getElementById('optionID').value;
	if (window.ActiveXObject) {
		xHRObject.open("GET","AJAX_servers/AJAX_image_swapper_server.php?action=loadImages&product_id=" + productID + "&attribute_id=" + attributeID + "&option_id=" + optionID + "&id=" + Number(new Date), true);	
		xHRObject.onreadystatechange = getData;
	}
	else if (window.XMLHttpRequest) {
		xHRObject.onreadystatechange = getData;
		xHRObject.open("GET","AJAX_servers/AJAX_image_swapper_server.php?action=loadImages&product_id=" + productID + "&attribute_id=" + attributeID + "&option_id=" + optionID + "&id=" + Number(new Date), true);	
	}
	xHRObject.send(null);
}

function displayImageName() {
	var imageName = document.getElementById('smallImage').value;
	imageName = imageName.slice(imageName.lastIndexOf('\\')+1);
	var selectElement = document.getElementById('imageName');
	selectElement.innerHTML = imageName;
	var selectElement = document.getElementById('title');
	selectElement.value = imageName;
}

function selectImage() {
	var imageID = document.getElementById('imageList').value;
	var imageDisplay = document.getElementById('imageDisplay');
	imageDisplay.innerHTML = '<img src="images/indicator.gif">';
	if (window.ActiveXObject) {
		xHRObject.open("GET","AJAX_servers/AJAX_image_swapper_server.php?action=selectImage&image_id=" + imageID + "&id=" + Number(new Date), true);	
		xHRObject.onreadystatechange = getData;
	}
	else if (window.XMLHttpRequest) {
		xHRObject.onreadystatechange = getData;
		xHRObject.open("GET","AJAX_servers/AJAX_image_swapper_server.php?action=selectImage&image_id=" + imageID + "&id=" + Number(new Date), true);	
	}
	xHRObject.send(null);
}

function update() {
	var imageID = document.getElementById('imageList').value;
	var newOrder = document.getElementById('order').value;
	var newTitle = document.getElementById('newTitle').value;
	var optionID = document.getElementById('optionID').value;
	if (window.ActiveXObject) {
		xHRObject.open("GET","AJAX_servers/AJAX_image_swapper_server.php?action=update&new_title=" + newTitle + "&option_id=" + optionID + "&image_id=" + imageID + "&new_order=" + newOrder + "&id=" + Number(new Date), true);	
		xHRObject.onreadystatechange = getData;
	}
	else if (window.XMLHttpRequest) {
		xHRObject.onreadystatechange = getData;
		xHRObject.open("GET","AJAX_servers/AJAX_image_swapper_server.php?action=update&new_title=" + newTitle + "&option_id=" + optionID + "&image_id=" + imageID + "&new_order=" + newOrder + "&id=" + Number(new Date), true);	
	}
	xHRObject.send(null);
}

function delImage() {
	var imageID = document.getElementById('imageList').value;
	var optionID = document.getElementById('optionID').value;
	if (window.ActiveXObject) {
		xHRObject.open("GET","AJAX_servers/AJAX_image_swapper_server.php?action=delImage&option_id=" + optionID + "&image_id=" + imageID + "&id=" + Number(new Date), true);	
		xHRObject.onreadystatechange = getData;
	}
	else if (window.XMLHttpRequest) {
		xHRObject.onreadystatechange = getData;
		xHRObject.open("GET","AJAX_servers/AJAX_image_swapper_server.php?action=delImage&option_id=" + optionID + "&image_id=" + imageID + "&id=" + Number(new Date), true);	
	}
	xHRObject.send(null);
}